# Código Figura 2

library(readxl)
library(dplyr)
library(ggplot2)

# ===== 1) Caminhos =====

setwd("C://Users//gabri//Desktop//base de dados artigo revisão//Figuras")
output_eps <- "Most_Relevant_Sources_300dpi.eps"

# ===== 2) Leitura e preparo =====
df <- read_xlsx("Figura_2/Base_figura_2.xlsx", sheet = 1)

# A planilha traz os nomes 'Sources' e 'Articles' na primeira linha de dados.
# Forçamos os nomes das colunas e removemos a primeira linha.
names(df)[1:2] <- c("Sources","Articles")
df <- df[-1, ]

df <- df %>%
  mutate(Articles = suppressWarnings(as.numeric(Articles))) %>%
  filter(!is.na(Articles), !is.na(Sources))

# Top 10
top <- df %>% arrange(desc(Articles)) %>% slice(1:10)
# Inverter ordem para desenhar de baixo p/ cima
top <- top %>% arrange(Articles)

# ===== 3) Gráfico (ggplot2 base) =====
p <- ggplot(top, aes(x = Articles, y = factor(Sources, levels = top$Sources))) +
  geom_segment(aes(x = 0, xend = Articles, y = factor(Sources, levels = top$Sources),
                   yend = factor(Sources, levels = top$Sources)), linewidth = 0.4) +
  geom_point(
    aes(size = Articles),
    shape = 21,                   
    fill  = "#A6DCE3",            
    color = "#2CA3B9",            
    stroke = 0.3
  )  +
  geom_text(aes(label = Articles), vjust = 0.5, hjust = 0.5, size = 3) +
  labs(title = "Most Relevant Sources",
       x = "N. of Documents", y = "Sources") +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5, margin = margin(b = 8))
  ) +
  scale_size(range = c(3, 15), guide = "none")
p
# ===== 4) Exportar EPS 300 dpi =====
ggsave(filename = output_eps, plot = p, device = cairo_ps,
       width = 9, height = 5, units = "in", dpi = 300)

cat(paste0("Arquivo salvo: ", normalizePath(output_eps), "\n"))
